//
//  ZH_MallCategoryViewController.m
//  WisdomStar
//
//  Created by zhangliwen on 2018/10/26.
//  Copyright © 2018年 zhangliwen. All rights reserved.
//

#import "ZH_MallCategoryViewController.h"
#import "GC_GoodsTypesInfoView.h"
#import "GC_HPBannerTableViewCell.h"
#import "ZH_GoodsListTableViewCell.h"
#import "ZH_CommoditymanageViewController.h"
#import "ZH_MallOrderViewModel.h"
@interface ZH_MallCategoryViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,weak)UIView *bgView;
@property (nonatomic,weak)UIImageView *addressImage;
@property (nonatomic,weak)UILabel *addressLabel;
@property (nonatomic,weak)UIView *lineView;
@property (nonatomic,weak)UIButton *addressButton;
///商品类型数据
@property (nonatomic, weak) GC_GoodsTypesInfoView *goodsTypeView;
@property (nonatomic,weak)UITableView *tableView;
@property (nonatomic,strong)ZH_MallOrderViewModel *viewModel;
@end

@implementation ZH_MallCategoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUserInterface];
    //商城分类页面左侧大分类获取
    [self allCataArrayData];
}
- (void)cleanUpData{
    [self setViewModel:nil];
}
- (void)initUserInterface{
    self.navigationItem.title = @"分类";
    self.view.backgroundColor = kCommonBackgroudColor;
//    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.left.right.equalTo(self.view);
//        make.height.mas_equalTo(FITSCALE(45));
//    }];
//    [self.addressImage mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.equalTo(self.bgView);
//        make.left.equalTo(self.view).offset(FITSCALE(12));
//        make.width.mas_equalTo(FITSCALE(12));
//        make.height.mas_equalTo(FITSCALE(16));
//    }];
//    [self.addressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self.addressImage.mas_right).offset(FITSCALE(7));
//        make.right.equalTo(self.view).offset(FITSCALE(-70));
//        make.centerY.equalTo(self.bgView);
//    }];
//    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.view).offset(FITSCALE(10));
//        make.bottom.equalTo(self.bgView).offset(FITSCALE(-10));
//        make.right.equalTo(self.view).offset(FITSCALE(-67));
//        make.width.mas_equalTo(FITSCALE(1));
//    }];
//    [self.addressButton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.equalTo(self.bgView);
//        make.right.equalTo(self.view).offset(FITSCALE(-16));
//    }];
    [self.goodsTypeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(FITSCALE(9));
        make.left.equalTo(self.view);
        make.bottom.equalTo(self.view);
        make.width.mas_equalTo(WIDTH_TRANSFORMATION(160));
    }];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(FITSCALE(9));
        make.left.equalTo(self.goodsTypeView.mas_right).offset(FITSCALE(11));
        make.right.equalTo(self.view).offset(FITSCALE(-11));
        make.bottom.equalTo(self.view);
    }];
}
#pragma mark - DATA
//商城分类页面左侧大分类获取
- (void)allCataArrayData{
    WEAKSELF;
    [self.viewModel allCateResultBlock:^(NSString *code, NSString *message, id data) {
        if ([code isEqualToString:@"0"]) {
            [self.goodsTypeView updateUIForData:self.viewModel.allCateArray];
        }
        else{
            if ([code isEqualToString:@"1002"]) {
                [self showHint:@"登录过期"];
            }
            else{
                [weakSelf showHint:message];
            }
        }
    }];
}
//商城分类右侧数据获取
- (void)allCataSonArrayData{
    WEAKSELF;
    [self.viewModel cateSonResultBlock:^(NSString *code, NSString *message, id data) {
        if ([code isEqualToString:@"0"]) {
            [weakSelf.tableView reloadData];
        }
        else{
            if ([code isEqualToString:@"1002"]) {
                [self showHint:@"登录过期"];
            }
            else{
                [weakSelf showHint:message];
            }
        }
    }];
}
#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1 + self.viewModel.cateSonArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(section > 0){
        return FITSCALE(39);
    }
    return CGFLOAT_MIN;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section > 0) {
        UIView *view = [UIView createViewWithBackgroundColor:kFontColorWhite];
        UIView *lineView = [UIView createViewWithBackgroundColor:[UIColor colorWithHexString:@"#00C9AE"]];
        [view addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(view);
            make.left.equalTo(view).offset(FITSCALE(12));
            make.width.mas_equalTo(FITSCALE(1));
            make.height.mas_equalTo(FITSCALE(17));
        }];
        UILabel *label = [UILabel createLabelWithText:self.viewModel.cateSonArray[section - 1][@"cate_name"] font:kFontSize_26];
        [view addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(view);
            make.left.equalTo(lineView.mas_right).offset(FITSCALE(7));
        }];
        return view;
    }
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        GC_HPBannerTableViewCell *cell = [GC_HPBannerTableViewCell cellWithTableView:tableView];
        [cell setPicHeight:FITSCALE(82)];
        [cell updateUIForImages:nil andDefaultImage:@"img_banner"];
        return cell;
    }
    else{
        ZH_GoodsListTableViewCell *cell = [ZH_GoodsListTableViewCell cellWithTableView:tableView];
        [cell updateUiForData:self.viewModel.cateSonArray[indexPath.section - 1][@"son"]];
        WEAKSelf;
        cell.onClickBlock = ^(NSString *idString) {
            ZH_CommoditymanageViewController *vc = [[ZH_CommoditymanageViewController alloc] init];
            vc.idString = idString;
            vc.hidesBottomBarWhenPushed = YES;
            [weakSelf.navigationController pushViewController:vc animated:YES];
        };
        return cell;
    }
    return nil;
}
#pragma mark - getter,setter
- (UIView *)bgView{
    if (!_bgView) {
        UIView *view = [UIView createViewWithBackgroundColor:kFontColorWhite];
        [self.view addSubview:_bgView = view];
    }
    return _bgView;
}
- (UIImageView *)addressImage{
    if (!_addressImage) {
        UIImageView *image = [UIImageView createImageViewWithName:@"icon_Location"];
        [self.view addSubview:_addressImage = image];
    }
    return _addressImage;
}
- (UILabel *)addressLabel{
    if (!_addressLabel) {
        UILabel *label = [UILabel createLabelWithText:@"成都市高新区xxx" font:kFontSize_26];
        [self.view addSubview:_addressLabel = label];
    }
    return _addressLabel;
}
- (UIView *)lineView{
    if (!_lineView) {
        UIView *view = [UIView createViewWithBackgroundColor:[UIColor colorWithHexString:@"#DFDFDF"]];
        [self.view addSubview:_lineView = view];
    }
    return _lineView;
}
- (UIButton *)addressButton{
    if (!_addressButton) {
        UIButton *button = [UIButton createNoBgButtonWithTitle:@"导航" target:self action:nil];
        [button setTitleColor:[UIColor colorWithHexString:@"#00C9AE"] forState:UIControlStateNormal];
        button.titleLabel.font = kFontSize_30;
        [self.view addSubview:_addressButton = button];
    }
    return _addressButton;
}
- (GC_GoodsTypesInfoView *)goodsTypeView
{
    if(!_goodsTypeView){
        GC_GoodsTypesInfoView *view = [[GC_GoodsTypesInfoView alloc] init];
        
        WEAKSelf;
        view.onSelectedBlock = ^(NSString *goodsTypeId) {
            [weakSelf.viewModel.cateSonParam setObject:goodsTypeId forKey:@"id"];
            [self allCataSonArrayData];
        };
        [self.view addSubview:_goodsTypeView = view];
    }
    return _goodsTypeView;
}
- (UITableView *)tableView{
    if (!_tableView) {
        UITableView *tableView = [UITableView createTableViewWithDelegateTarget:self];
        tableView.backgroundColor = kCommonWhiteBg;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [tableView setCellAutoAdaptationForEstimatedRowHeight:FITSCALE(100)];
        [self.view addSubview:_tableView = tableView];
    }
    return _tableView;
}
- (ZH_MallOrderViewModel *)viewModel{
    if (!_viewModel) {
        _viewModel = [[ZH_MallOrderViewModel alloc] init];
    }
    return _viewModel;
}
@end
